/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wisnu.spms.source.model.tableModel;

import com.wisnu.spms.service.entity.DbCashlog;
import com.wisnu.spms.service.entity.DbPayment;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Wisnu Wardoyo
 */
public class PanelPaymentTableModel extends AbstractTableModel {

    List<DbPayment> list = new ArrayList<>();

    public void setList(List<DbPayment> list) {
        this.list = list;
    }

    public boolean add(DbPayment element) {
        try {
            return list.add(element);
        } finally {
            fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
        }
    }

    public DbPayment get(int i) {
        return list.get(i);
    }

    public DbPayment set(int index, DbPayment element) {
        try {
            return list.set(index, element);
        } finally {
            fireTableRowsUpdated(index, index);
        }
    }

    public DbPayment remove(int index) {
        try {
            return list.remove(index);
        } finally {
            fireTableRowsDeleted(index, index);
        }
    }

    @Override
    public int getRowCount() {
        return list.size();
    }

    @Override
    public int getColumnCount() {
        return 5;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case 0:
                return rowIndex+1;
            case 1:
                return list.get(rowIndex).getDbBilling().getDbBillingtype().getBillingTypeName();
            case 2:
                return list.get(rowIndex).getDbBilling().getDbBillingtype().getBillingAmount();
            case 3:
                return list.get(rowIndex).getPaymentDate();
            case 4:
                for (Iterator<DbCashlog> it = list.get(rowIndex).getDbCashlogs().iterator(); it.hasNext();) {
                    DbCashlog cashlog = it.next();
                    return cashlog.getDbEmployee().getEmployeeName();
                }
            default:
                return null;
        }
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0:
                return "No";
            case 1:
                return "Pembayaran";
            case 2:
                return "Jumlah";
            case 3:
                return "Tanggal";
            case 4:
                return "Operator";
            default:
                return null;
        }
    }

}
